package com.tronsis.distribution.fragment;

import android.Manifest;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.me.ProfileActivity;
import com.tronsis.distribution.activity.me.QrQueryActivity;
import com.tronsis.distribution.activity.me.StockProcessingActivity;
import com.tronsis.distribution.activity.me.address.AddressManageActivity;
import com.tronsis.distribution.activity.me.agent.AgentExitProcessingActivity;
import com.tronsis.distribution.activity.me.agent.AgentRecruitingActivity;
import com.tronsis.distribution.activity.me.complaints.ComplaintsActivity;
import com.tronsis.distribution.activity.me.coupons.CouponsActivity;
import com.tronsis.distribution.activity.me.order.MyOrdersActivity;
import com.tronsis.distribution.activity.me.points.MyPointsActivity;
import com.tronsis.distribution.activity.me.points.PointProcessingActivity;
import com.tronsis.distribution.activity.me.stock.LowerStockActivity;
import com.tronsis.distribution.activity.me.stock.StockActivity;
import com.tronsis.distribution.activity.me.upgrade.LevelApplyActivity;
import com.tronsis.distribution.activity.me.upgrade.ReturnApplyListActivity;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.distribution.util.StateUtil;
import com.tronsis.library.fragment.BaseFragment;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.ImageLoader;
import com.tronsis.library.permission.EasyPermissions;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.widget.CircleImageView;
import com.tronsis.library.zxing.CaptureActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MeFragment extends BaseFragment {

    @BindView(R.id.iv_cover)
    CircleImageView mIvCover;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_level)
    TextView mTvLevel;
    @BindView(R.id.tv_code)
    TextView mTvCode;
    @BindView(R.id.ll_return_apply)
    LinearLayout mLlReturnApply;
    @BindView(R.id.ll_switch_agent)
    LinearLayout mLlSwitchAgent;
    @BindView(R.id.view_line_return_apply)
    View mViewReturnApply;
    @BindView(R.id.view_line_switch_agent)
    View mViewSwitchAgent;
    private String id;  //用户ID

    @Override
    protected View onCreateView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_me, null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        UserBean userBean = UserBizFactory.createBiz().getLoginUser(mContext);
        setInfoView(userBean);
        httpGetUserInfo();
    }

    private void setInfoView(UserBean userBean) {
        if (userBean == null) {
            return;
        }
        ImageLoader.displayAvatar(mContext, Url.FILE_DOWNLOAD + userBean.getAvatarFid(), mIvCover);
        mTvName.setText(TextUtils.isEmpty(userBean.getName()) ? userBean.getPhone() : userBean.getName());
        mTvLevel.setText(StateUtil.getLevelText(userBean.getLevel(), userBean.isUnited()));
        mTvCode.setText(userBean.getCode());

        if (StringUtil.isEquals(userBean.getRole(), StateUtil.ROLE_UO_AGENT)) {
            mLlReturnApply.setVisibility(View.GONE);
            mViewReturnApply.setVisibility(View.GONE);
            mLlSwitchAgent.setVisibility(View.VISIBLE);
            mViewSwitchAgent.setVisibility(View.VISIBLE);
        }else{
            mLlReturnApply.setVisibility(View.VISIBLE);
            mViewReturnApply.setVisibility(View.VISIBLE);
            mLlSwitchAgent.setVisibility(View.GONE);
            mViewSwitchAgent.setVisibility(View.GONE);
        }

    }

    @OnClick(R.id.ll_profile)
    void profileClick() {
        //个人信息
        Intent intent = new Intent(mContext, ProfileActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_stock)
    void stockClick() {
        //库存
        Intent intent = new Intent(mContext, StockActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_my_order)
    void myOrderClick() {
        //我的订单
        Intent intent = new Intent(mContext, MyOrdersActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_agent_recruiting)
    void agentRecruitingClick() {
        //代理招聘
        Intent intent = new Intent(mContext, AgentRecruitingActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_my_points)
    void myPointsClick() {
        //我的积分
        Intent intent = new Intent(mContext, MyPointsActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_coupons)
    void couponsClick() {
        //优惠券
        Intent intent = new Intent(mContext, CouponsActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_level_apply)
    void levelApplyClick() {
        //升降级申请
        Intent intent = new Intent(mContext, LevelApplyActivity.class);
        intent.setType("level");
        startActivity(intent);
    }

    // allen : 退押申请
    @OnClick(R.id.ll_return_apply)
    void returnApplyClick() {
        //退押申请
        Intent intent = new Intent(mContext, ReturnApplyListActivity.class);
        intent.setType("level");
        startActivity(intent);
    }

    @OnClick(R.id.ll_switch_agent)
    void switchAgentClick() {
        //转为代理
        Intent intent = new Intent(mContext, LevelApplyActivity.class);
        intent.setType("switch");
        startActivity(intent);
    }

    @OnClick(R.id.ll_address_manage)
    void addressManageClick() {
        //地址管理
        Intent intent = new Intent(mContext, AddressManageActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_complaints)
    void complaintsClick() {
        //投诉
        Intent intent = new Intent(mContext, ComplaintsActivity.class);
        startActivity(intent);
    }

    // allen : 下级库存按钮
    @OnClick(R.id.ll_lower_stock)
    void lowerStockClick() {
        //下级库存
        Intent intent = new Intent(mContext, LowerStockActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    @OnClick(R.id.ll_qr_query)
    void qrQueryClick() {
        //扫码查询
        Intent intent = new Intent(mContext, QrQueryActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_stock_processing)
    void stockPurchaseClick() {
        //进货处理
        Intent intent = new Intent(mContext, StockProcessingActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_qr_delivery)
    void qrDeliveryClick() {
        //扫码发货
        if (EasyPermissions.hasPermissions(mContext, Manifest.permission.CAMERA)) {
            Intent intent = new Intent(mContext, CaptureActivity.class);
            startActivity(intent);
        } else {
            EasyPermissions.requestPermissions(mContext, getString(R.string.ask_again), 0x151, Manifest.permission.CAMERA);
        }
    }

    @OnClick(R.id.ll_point_processing)
    void pointProcessingClick() {
        //积分处理
        Intent intent = new Intent(mContext, PointProcessingActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_exit_agent_processing)
    void agentExitProcessingClick() {
        //退出代理处理
        Intent intent = new Intent(mContext, AgentExitProcessingActivity.class);
        startActivity(intent);
    }


    private void httpGetUserInfo() {
        try {
            UserBizFactory.createBiz().getUserInfo(getActivity(), new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    UserBean userBean = (UserBean) data;
                    id = userBean.getId();
                    setInfoView(userBean);
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
